package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {

    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";

    public static final String EXPRESSION_IN_SESSION = "EXPRESSION_IN_SESSION";

    //  获取session对象
    public static HttpSession getSession(){
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        return attributes.getRequest().getSession();
    }

    //  往session存入登录用户
    public static void setCurrentUser(Employee employee) {
        getSession().setAttribute(EMPLOYEE_IN_SESSION, employee);
    }

    //  从session获取登录用户
    public static Employee getCurrentUser(){
        return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }

    //  往session存入登录用户的权限信息
    public static void setExpression(List<String> permissions){
        getSession().setAttribute(EXPRESSION_IN_SESSION, permissions);
    }

    //  从session获取登录用户的权限信息
    public static List<String> getExpression(){
        return (List<String>) getSession().getAttribute(EXPRESSION_IN_SESSION);
    }
}
